package cn.ljy.uem.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.SchoolCourseResourceMapper;
import cn.ljy.uem.dao.SchoolCourseSectionMapper;
import cn.ljy.uem.dao.SchoolCourseSectionResourceMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.model.SchoolCourseSection;
import cn.ljy.uem.model.SchoolCourseSectionResource;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolCourseSectionResourceQurey;
import cn.ljy.uem.service.SchoolCourseSectionResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * 学校课程资源章节实现类
 *
 * @author 小锋
 * @date 2022/08/24
 */
@Service
@Slf4j
public class ISchoolCourseSectionResourceService extends IBaseService<SchoolCourseSectionResource, SchoolCourseSectionResourceQurey> implements SchoolCourseSectionResourceService {
    @Autowired
    SchoolCourseSectionResourceMapper schoolCourseSectionResourceMapper;
    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;
    @Autowired
    private SchoolCourseSectionMapper schoolCourseSectionMapper;
    @Autowired
    private SchoolCourseResourceMapper schoolCourseResourceMapper;

    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(SchoolCourseSectionResource schoolCourseSectionResource) {

    }

    @Override
    protected void beforeSave(SchoolCourseSectionResource schoolCourseSectionResource) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }

    @Override
    public boolean uploadSectionResources(String sectionId, Long[] resourceIds){
        //上传是否成功
        boolean result=false;
        //获取当前登录账户
        SysUser sysUser= UserUtil.getLoginUser();
        //查找当前账户对应的教师
        SchoolTeacher schoolTeacher=schoolTeacherMapper.getTeacherByUserId(sysUser.getId());
        if(schoolTeacher!=null){
            //将章节和资源的关系添加进表
            if(resourceIds.length>0){
                //要批量添加的资源
                ArrayList<SchoolCourseSectionResource> schoolCourseSectionResources = new ArrayList<>();
                for(Long resourceId:resourceIds){
                    if(resourceId!=null){
                        SchoolCourseSectionResource schoolCourseSectionResource=new SchoolCourseSectionResource();
                        schoolCourseSectionResource.setSectionId(sectionId);
                        schoolCourseSectionResource.setResourceId(resourceId);
                        schoolCourseSectionResource.setCreator(schoolTeacher.getId());
                        //添加到集合中
                        schoolCourseSectionResources.add(schoolCourseSectionResource);
                    }
                }
                //批量插入数据
                Integer i=schoolCourseSectionResourceMapper.saveAllIn(schoolCourseSectionResources);
                if(i>0){ // 最佳判断是等于上传资源
                    result=true;
                }
            }else {
                throw new MyWebException("上传失败！");
            }
        }else {
            throw new MyWebException("教师账户才可以上传");
        }
        return result;
    }


    @Override
    protected void afterFindAll(List<SchoolCourseSectionResource> list) {
        for (SchoolCourseSectionResource sectionResource : list) {
            if(sectionResource.getSectionId() != null){
                sectionResource.setSchoolCourseSection(schoolCourseSectionMapper.findById(sectionResource.getSectionId()));
            }
            if(sectionResource.getResourceId() != null){
                sectionResource.setSchoolCourseResource(schoolCourseResourceMapper.findById(sectionResource.getResourceId()));
            }
        }
    }

    @Override
    protected void afterGet(SchoolCourseSectionResource schoolCourseSectionResource) {
        if(schoolCourseSectionResource.getSectionId() != null){
            schoolCourseSectionResource.setSchoolCourseSection(schoolCourseSectionMapper.findById(schoolCourseSectionResource.getSectionId()));
        }
        if(schoolCourseSectionResource.getResourceId() != null){
            schoolCourseSectionResource.setSchoolCourseResource(schoolCourseResourceMapper.findById(schoolCourseSectionResource.getResourceId()));
        }
    }
}
